package com.shijunhao.graduation.handler.auth;

import com.alibaba.fastjson.JSON;
import com.shijunhao.graduation.domain.bean.ResultCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: shijunhao
 * @date: 2022/3/3 15:04
 * @description:
 */
public class NoneAuthHandlerImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", MediaType.ALL.getSubtype());
        response.getWriter().write(JSON.toJSONString(ResultCode.resultCode(HttpStatus.UNAUTHORIZED.value(),"请先完成登录！")));
    }
}
